package com.ruipeng.shengyapo.controller;

import com.ruipeng.shengyapo.comm.ResponseData;
import com.ruipeng.shengyapo.model.UserInfo;
import com.ruipeng.shengyapo.service.AuthService;
import com.ruipeng.shengyapo.service.OrderService;
import com.ruipeng.shengyapo.service.UserService;
import com.ruipeng.shengyapo.utils.ContentUtils;
import com.ruipeng.shengyapo.utils.ObjUtils;
import com.ruipeng.shengyapo.utils.PhoneFormatCheckUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tu_pei on 17/5/27.
 * 登录验证
 */

@Controller
@RequestMapping("auth")
public class AuthController {

    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;

    /**
     * 验证手机号和验证码
     * url/auth/userTel/code
     * @param userTel 手机号
     * @param code 验证码
     * @return
     */
    @RequestMapping(value = "/{userTel}/{code}",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData userRegister(@PathVariable String userTel,@PathVariable String code){
        ResponseData data = new ResponseData();
        if( !PhoneFormatCheckUtils.isPhoneLegal(userTel)){
            //手机号格式不正确
            data.setMsg("手机号格式不正确");
            data.setState(ContentUtils.COMM_STATE_FALSE);
            return data;
        }else{
            //获取该手机号信息
            Map<String,Object> userMap = userService.getByUserTel(userTel);
            if( userMap != null ){
                //验证码
                String phoneCode = (String)userMap.get("phoneCode");
                if( !code.equals( phoneCode ) ){
                    //验证码错误
                    data.setMsg("验证码错误");
                    data.setState(ContentUtils.COMM_STATE_FALSE);
                    return data;
                }else{
                    data.setResults(userMap);
                    data.setState(ContentUtils.COMM_STATE_TRUE);
                    return data;
                }
            }else{
                data.setMsg("该用户不存在");
                data.setState(ContentUtils.COMM_STATE_FALSE);
                return data;
            }
        }
    }

    /**
     * 校验未完成的订单
     * @param userId
     * @return
     */
    @RequestMapping(value = "/{userId}/notCompleteOrder",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData notCompleteOrder(@PathVariable int userId,String version){
        ResponseData data = new ResponseData();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("userId",userId);
        List<Map<String,Object>> orderInfo = orderService.getUserOrderByUserId(params);
        if( orderInfo.size() > 0 ){
            data.setState(ContentUtils.COMM_STATE_FALSE);
            data.setResults(orderInfo.get(0));
            data.setMsg("您有未完成的订单");
            return data;
        }
        else{
            data.setState(ContentUtils.COMM_STATE_TRUE);
            return data;
        }


    }

    /**
     * 检测版本更新
     * @param version
     * @return
     */
    @RequestMapping(value = "/checkVersion",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData checkVersion(String version){
        ResponseData data = new ResponseData();
        if(ObjUtils.isEmpty(version) || !version.equals("2")){
            Map<String,Object> versionMap = new HashMap<String,Object>();
            versionMap.put("url","http://syb.ruipengwang.com:8082/syb_admin/download/syb-2.apk");
            versionMap.put("isUpdate","1");
            data.setState(ContentUtils.COMM_STATE_FALSE);
            data.setResults(versionMap);
            return data;
        }else{
            data.setState(ContentUtils.COMM_STATE_TRUE);
            return data;
        }
    }

}
